package com.example.shiwuyouproject.ui.fragment.my.activity

import android.content.Intent
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.RegexUtils
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityChangepasswordBinding
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.ChangePasswordViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.example.shiwuyouproject.utils.CountDownTimerUtils
import com.example.shiwuyouproject.utils.SPUtils
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils

/**
 * 修改密码
 */
class ChangePasswordActivity :
    BaseVmActivity<ActivityChangepasswordBinding, ChangePasswordViewModel>() {
    override fun viewModelClass(): Class<ChangePasswordViewModel> =
        ChangePasswordViewModel::class.java

    override fun getViewBinding(): ActivityChangepasswordBinding {
        return ActivityChangepasswordBinding.inflate(layoutInflater)
    }

    private var mCountDownTimerUtils: CountDownTimerUtils? = null
    private var stTitle = ""
    override fun initView() {
        stTitle = intent.getStringExtra("stTitle").toString()
        mBinding.titleBar.tvTitle.text = stTitle
        if (SPUtils.getInstance().myUserInfo != null) {
            mBinding.etUsername.setText(SPUtils.getInstance().myUserInfo.mobile)
        }
        if (intent.getStringExtra("phone") != null) {
            mBinding.etUsername.setText(intent.getStringExtra("phone").toString())
        }
        mCountDownTimerUtils = CountDownTimerUtils(mBinding.tvCode, 60000, 1000)
    }

    override fun initData() {


    }


    override fun setListener() {
        mBinding.titleBar.tvBack.setOnClickListener { finish() }
        mBinding.tvCode.setOnClickListener {
            val username = mBinding.etUsername.text.toString().trim()
            if (username.isBlank()) {
                ToastUtil.show("手机号码不能为空！")
                return@setOnClickListener
            }
            if (!RegexUtils.isMobileSimple(username)) {
                ToastUtil.show("请输入正确手机号！")
                return@setOnClickListener
            }
            YUtils.showLoading(this, "获取中...")
            mViewModel.smsSend(username)
        }
        mBinding.btRegister.setOnClickListener {
            val username = mBinding.etUsername.text.toString().trim()
            val etCode = mBinding.etCode.text.toString()
            if (username.isBlank()) {
                ToastUtil.show("手机号码不能为空！")
                return@setOnClickListener
            }
            if (!RegexUtils.isMobileSimple(username)) {
                ToastUtil.show("请输入正确手机号！")
                return@setOnClickListener
            }
            if (etCode.isEmpty()) {
                ToastUtil.show("请输入验证码！")
                return@setOnClickListener
            }
            if (mBinding.etPwd1.text.isEmpty() || mBinding.etPwd2.text.isEmpty()) {
                ToastUtil.show("请输入密码！")
                return@setOnClickListener
            }
            if (mBinding.etPwd1.text.toString() != mBinding.etPwd2.text.toString()) {
                ToastUtil.show("两次密码输入不一致！")
                return@setOnClickListener
            }
            YUtils.showLoading(this, "密码修改中...")
            mViewModel.getUserResetpwd(username, mBinding.etPwd1.text.toString(), etCode)
        }

    }

    override fun observe() {
        mViewModel.messageStatus.observe(this, Observer<String>{
            ToastUtil.show(it)
            YUtils.hideLoading()
        })
        mViewModel.myCodeState.observe(this, Observer<Boolean>{
            YUtils.hideLoading()
            if (it) {
                mCountDownTimerUtils?.start()
            }
        })
        mViewModel.getUserResetpwdState.observe(this,  Observer<Boolean>{
            YUtils.hideLoading()
            if (it) {
                ToastUtil.show("修改成功！")
                val mIntent = Intent()
                mIntent.putExtra("phone", mBinding.etUsername.text.toString())
                mIntent.putExtra("pwd", mBinding.etPwd1.text.toString())
                setResult(100, mIntent)
                finish()
            }
        })
    }
}